/* 
 * Copyright 2010 Ziya Guven Koylu, Ozge Koylu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.koylu.caffein.query.filter;

import java.sql.PreparedStatement;
import java.util.Map;
import java.util.StringTokenizer;

import com.koylu.caffein.model.caffein.CaffeinConfig;
import com.koylu.caffein.model.clazz.Clazz;


public abstract class Filter {
	
	public static And and(Filter... filters){
		return new And(filters);
	}
	
	public static Or or(Filter... filters){
		return new Or(filters);
	}
	
	public static Filter eq(String alias, String path, Object value){
		return new Eq(alias, path, value);
	}
	
	public static Filter neq(String alias, String path, Object value){
		return new Neq(alias, path, value);
	}
	
	public static Filter gt(String alias, String path, Object value){
		return new Gt(alias, path, value);
	}
	
	public static Filter gte(String alias, String path, Object value){
		return new Gte(alias, path, value);
	}
	
	public static Filter lt(String alias, String path, Object value){
		return new Lt(alias, path, value);
	}
	
	public static Filter lte(String alias, String path, Object value){
		return new Lte(alias, path, value);
	}
	
	public static Filter like(String alias, String path, Object value){
		return new Like(alias, path, value);
	}
	
	public static Filter between(String alias, String path, Object firstValue, Object secondValue){
		return new Between(alias, path, firstValue, secondValue);
	}
	
	public static Filter in(String alias, String path, Object... value){
		return new In(alias, path, value);
	}
	
	public static Filter isnull(String alias, String path){
		return new IsNull(alias, path);
	}
	
	public static Filter notnull(String alias, String path){
		return new NotNull(alias, path);
	}
	
	public abstract StringBuffer toQuery(CaffeinConfig caffeinConfig, Map<String, Clazz> aliasMap) throws Exception;
	public abstract int fieldToColumn(CaffeinConfig caffeinConfig, PreparedStatement preparedStatement, int index) throws Exception;
	public abstract void resolveValue(Object object) throws Exception;
	public static String getToken(String str){
		if(str != null){
			StringTokenizer token = new StringTokenizer(str,"#{");
			while(token.hasMoreTokens()){
				String tokenStr = token.nextToken();
				if(tokenStr.indexOf("}") > -1){
					return tokenStr.substring(0,tokenStr.indexOf("}"));	
				}		
			}
		}
		return null;
	}
}